const app = getApp()

import { HiddenCall } from '../../request/api.js'
const request = new HiddenCall
import { throttle } from '../../utils/throttle.js'
import { makePhoneCall } from "../../utils/http.js"
const behavior = require('../relation_behavior')
Component({
  /**
*  信息列表
* - @author           王磊
* - @date             2018-07-04
* list                      消息列表数据
* screenHeight     屏幕的高度
* scrollBarTop      滚动条的高度
*/
  behaviors: [behavior],
  properties: {
    list: {
      type: Object
    },
    screenHeight: Number,
    scrollBarTop: Number,
    messageList: Object,
    loading: Boolean
  },
  methods: {
  /**
   * 消息列表显示更多||隐藏更多
   * author wl
   */
    isShowCode(e) {
      // 获取当前点击的索引
      let index = e.currentTarget.dataset.index
      // 消息列表数据
      let list = this.data.list.content
      for (let i = 0; i < list.length; i++) {
        // 如果有更多的数据给当前点击的更多数据checked为true
        if (list[i].messageDetails) {
          index === i ? list[i].checked = !list[i].checked : list[i].checked = false
        }
      }
      // 数据更新
      this.setData({ list: this.data.list })
    },
    /**
     * 进入房客源详情
     * author wl
     */
    entrMessageDetail: throttle(function(e) {
      // 房客源编号
      let resourceCode = e.currentTarget.dataset.detail[0],
        // 房源客源标志
        tacticsType = e.currentTarget.dataset.detail[1],
        readId = e.currentTarget.dataset.detail[2]
      request.readMessage(readId).then(res => { })
      if (!resourceCode) {
        wx.showToast({ title: tacticsType==='house'?'此房源已删除！':'此客源已删除!', icon: 'none'})
        return
      }
      // 数据汇集
      let model = {
        resourceCode,
        tactics: tacticsType === "house"? "houseServiceImpl" : "customerServiceImpl",
        secondaryType: "index"
      }
      // 根据房客源跳转到对应的详情页面
      wx.navigateTo({
        url: `../../pages/${tacticsType === "house" ? "house_detail" : "customer_detail"}/index?model=${JSON.stringify(model)}`
      })
    }, 1000),
  /**
  * 消息列表拨号键页面拨打电话
  * author dzl
  */
  msgMakePhone: throttle(function (e) {
    let that = this
    let called = e.currentTarget.dataset.message
    let caller = app.globalData.phonenumber
    let id = e.currentTarget.dataset.id
    id ? request.readMessage(id).then(res => { }) : ""
    // 拨打电话传入拨打手机号和被呼叫手机号
    request.directCall({ called }).then(res => {
      // 拨打电话，跳转所有列表
      that.phoneCall(res)
      called = ''
    })
  }, 2000),
    /**
   * 消息列表拨打电话，所有列表详情拨打电话
   * callId                 电话id
   * phonenumber  电话号码
   */
    phoneCall(res) {
      if (res.statusCode === 200) {
        wx.hideLoading()
        // 拿到电话
        let callId = res.result.callId
        app.phonenumber = res.result.switchNumber
        // 拨打电话进行跳转
        makePhoneCall().then(res => {
          let model = {}
          model.callId = callId
          // 跳转到电话列表页
        }).catch(() => {
          // this.clickMask()
        })
        // wx.showLoading({ title: '设置拨打关系中', icon: 'loading' })
      }
      res.message !== "成功" ? wx.showToast({
        title: res.message,
        icon: 'none'
      }):""
    },
    /**
     * 通知来电 进入发送短信页面
     * @auther wl
     */
    messageSms (e) {
      let phone = e.currentTarget.dataset.message
      let data = {
        callType: "sms",
        detailType: "allDetail",
        callModel: { customerPhone: phone }
      }
      // 进入短信详情带入参数
      wx.navigateTo({
        url: `../../pages/mailing_detail/index?model=${JSON.stringify(data)}`
      })
    },
    notEnter () {
      // this.setData({ EnableCall: true})
    },
    closeNotEnter () {
      this.setData({ EnableCall: false })
    },
    /**
     * 获取解除限制的手机号
     */
    enableValue(e) {
      this.setData({
        enableInput: e.detail.value
      })
    },
    /**
    * 取消限制拨打方法
    */
    cancelEnable: function (e) {
      // 调用解除限制拨打传入限制手机号
      request.freeCall({ called: this.data.enableInput, caller: app.globalData.caller, resourceCode: this.data.code }).then(res => {
        // 关闭限制弹窗
        this.closeNotEnter()
      })
    }
  }
})
